<?php
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
//声明页面上方tab按钮
$return['goods_types']=$this->goods_types;
//声明列表页
if($_RQ['op']=='list'){
	//初始化返回前端的筛选条件数组
	$return['params']=array();
	//整理列表数据查询条件
	$params=array('uniacid'=>$_SESSION['uniacid'],'depot_id'=>$depot_id);
	if($_RQ['title']){
		$goods_ids=$this->pdo_getall('goods',array('uniacid'=>$_SESSION['uniacid'],'title like'=>'%'.trim($_RQ['title']).'%'),array('id'),'id');
		if($goods_ids){
			$params['goods_id']=array_keys($goods_ids);
		}else{
			$params['goods_id']=0;
		}
		$return['params']['title']=$_RQ['title'];
	}
	$psize=15;
	$pindex=intval($_RQ['page'])?:1;
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'orderby'=>'id desc',
	);
	//执行分页查询
	$list=$this->get_list('depot_goods',$parameter,$total);
	foreach($list as &$item){
		if($item['goodstype']==1){
				$goods=$this->pdo_get('goods',array('id'=>$item['goods_id']),array('title','image','productsn'));
		}else{
				$goods=pdo_get('jy_weishop_goods',array('id'=>$item['goods_id']),array('title','image','productsn'));
		}
		$item=array_merge($goods,$item);
	}
	//将查询到的结果数组放入$return以备传回前端
	$return['list']=$list;
	//整理分页
	$return['totalpage'] =intval(ceil($total/$psize));
	$return['params']['page'] =$pindex;
	//声明列表页样式数组
	$htmls=array(
		'type'=>'list',
		//'export'=>'export',//声明导出动作op，如无需导出功能，可不声明该字段
		'list'=>array('data'=>'list','edit'=>false,'delall'=>false,'del_one'=>false,'list'=>array(
			//列表展示列
			array('type'=>'radio','name'=>'type','title'=>'商品类型','options'=>'goods_types'),
			array('type'=>'string','name'=>'title','title'=>'商品名称',),
			array('type'=>'image','name'=>'image','title'=>'商品图片',),
			array('type'=>'string','name'=>'productsn','title'=>'商品编号',),
			array('type'=>'string','name'=>'total','title'=>'当前库存',),
		)),
		'search'=>array(
			//列表查询表单
			array('type'=>'string','name'=>'title','title'=>'名称','width'=>'50'),
		),
	);
	$return['htmls']=$htmls;
	
	//把数据返回前端
	exi($return);
}
if($_RQ['op']=='choose'){
	//初始化返回前端的筛选条件数组
	$return['params']=array();
	//整理列表数据查询条件
	$params=array('uniacid'=>$_SESSION['uniacid'],'depot_id'=>$depot_id);
	if($_RQ['title']){
		$goods_ids=pdo_getall('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'title like'=>'%'.trim($_RQ['title']).'%'),array('id'),'id');
		if($goods_ids){
			$params['goods_id']=array_keys($goods_ids);
		}else{
			$params['goods_id']=0;
		}
		$return['params']['title']=$_RQ['title'];
	}
	$psize=15;
	$pindex=intval($_RQ['page'])?:1;
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'orderby'=>'id desc',
	);
	//执行分页查询
	$list=$this->get_list('depot_goods',$parameter,$total);
	foreach($list as &$item){
		if($item['goodstype']==1){
			$goods=$this->pdo_get('goods',array('id'=>$item['goods_id']),array('title','image','productsn'));
		}else{
			$goods=pdo_get('jy_weishop_goods',array('id'=>$item['goods_id']),array('total','title','image','productsn'));
			$this->pdo_update('depot_goods',array('total'=>$goods['total']),array('id'=>$item['id']));	
			$item['total']=$goods['total'];
		}
		$item=array_merge($goods,$item);
		$item['goods']=array(
				'id'=>$item['id'],
				'title'=>$item['title'],
				'image'=>$item['image'],
				'total'=>$item['total'],
		);
	}
	//将查询到的结果数组放入$return以备传回前端
	$return['list']=$list;
	//整理分页
	$return['totalpage'] =intval(ceil($total/$psize));
	$return['params']['page'] =$pindex;
	//声明列表页样式数组
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','edit'=>false,'delall'=>false,'del_one'=>false,'list'=>array(
			//列表展示列
			array('type'=>'radio','name'=>'goodstype','title'=>'商品类型','options'=>'goods_types'),
			array('type'=>'string','name'=>'title','title'=>'商品名称',),
			array('type'=>'image','name'=>'image','title'=>'商品图片',),
			array('type'=>'string','name'=>'productsn','title'=>'商品编号',),
			array('type'=>'string','name'=>'total','title'=>'当前库存',),
		),'action'=>array(
			array('type'=>'modal_result','name'=>'goods','title'=>'选取'),
		)),
		'search'=>array(
			//列表查询表单
			array('type'=>'string','name'=>'title','title'=>'名称','width'=>'50'),
		),
	);
	$return['htmls']=$htmls;
	//把数据返回前端
	exi($return);
}

//导出动作
if($_RQ['op']=='export'){
	//整理筛选条件
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['title']){
		$params['title like']='%'.trim($_RQ['title']).'%';
	}
	//声明需要查询哪些字段，为空表示获取所有
	$fields=array();
	$parameter=array(
		'params'=>$params,
		'fields'=>$fields,
		'orderby'=>'id desc',
	);
	//执行分页查询
	$list=$this->get_list('goods',$parameter);
	if($list){
		foreach($list as &$item){
			//整理从数据库获取到的数据
			$item['createtime']=date('Y/m/d H:i:s',$item['createtime']);
		}
	}
	//声明表头和对应字段
	$header=array(
		'title'=>'门店名称',
		'address'=>'门店地址',
		'createtime'=>'门店创建时间',
	);
	//执行导出
	model('core/tools')->export2excel($header,$list,'门店列表');
}